package xx.services.demo.rest;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.demo.DemoLayout;
import xx.entity.demo.DemoLayoutItem;
import xx.services.Passport;
import xx.services.demo.inter.ILayoutInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * Demo布局模板管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/demo/layout")
 * @author Administrator
 * 
 * 2010-12-20
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/demo/layout")
public class LayoutRest extends Passport implements ILayoutInterface
{

    public LayoutRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 新增一个布局demo。 <br>
     * 
     * @POST
     * @param title-@FormParam("title")
     *            布局名称
     * @param container-@FormParam("container")
     *            布局容器，一般是一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            布局dome样例
     * @param shape-@FormParam("shape")
     *            布局样式图片
     * @param description-@FormParam("description")
     *            布局描述
     * @return 新增的布局实体
     * @author chenxiang
     */
    @POST
    public String add(@FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoLayout dl = new DemoLayout(qr, owner);
            dl.setTitle(title).setContainer(container);
            if (demo != null && !"".equals(demo.trim()))
            {
                dl.setDemo(demo);
            }
            if (shape != null && !"".equals(shape.trim()))
            {
                dl.setShape(shape);
            }
            if (description != null && !"".equals(description.trim()))
            {
                dl.setDescription(description);
            }
            dl.insert();
            return ok(dl);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 新增一个布局列 itam demo。 <br>
     * 
     * @POST
     * @Path("/{layout_id}/item")
     * 
     * @param layoutId-@PathParam("layout_id")
     *            布局ID
     * @param demo-@FormParam("demo")
     *            布局列DOME实例
     * @param templet-@FormParam("templet")
     *            布局列模板
     * @return 新增的列实体
     * @author chenxiang
     */
    @POST
    @Path("/{layout_id}/item")
    public String addItem(@PathParam("layout_id")
    long layoutId, @FormParam("demo")
    String demo, @FormParam("templet")
    String templet)
    {
        try
        {
            DemoLayoutItem dli = new DemoLayoutItem(qr, owner);
            dli.setLayoutId(layoutId);
            if (demo != null && !"".equals(demo.trim()))
            {
                dli.setDemo(demo);
            }
            if (templet != null && !"".equals(templet.trim()))
            {
                dli.setTemplet(templet);
            }
            dli.insert();
            return ok(dli);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 描述：删除布局demo。<br>
     * 
     * @DELETE
     * @Path("/{layout_id}")
     * @param layoutId-@PathParam("layout_id")
     *            布局ID，多个布局ID用“_”隔开
     * @return String 下午09:07:52
     * @author chenxiang
     */
    @DELETE
    @Path("/{layout_id}")
    public String delete(@PathParam("layout_id")
    String layoutId)
    {
        try
        {
            int count = 0;
            long[] layoutArr = Arr.toLongArray(layoutId, "_");
            for (long id : layoutArr)
            {
                DemoLayout dl = new DemoLayout(qr, owner);
                dl.setLayoutId(id);
                dl.delete();
                count++;
            }
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 描述：获取一个布局demo，包含Item<br>
     * 
     * @GET
     * @Path ("/{layout_id}")
     * @param layoutId-@PathParam("layout_id")
     *            布局ID
     * @return 布局实体，包含Item
     * @author chenxiang
     */
    @GET
    @Path("/{layout_id}")
    public String get(@PathParam("layout_id")
    long layoutId)
    {
        try
        {
            DemoLayout dl = new DemoLayout(qr, owner);
            dl.setLayoutId(layoutId).load();
            return ok(dl);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取布局列layout_item列表。<br>
     * 
     * @GET
     * @Path("/{layout_id}/item")
     * 
     * @param layoutId-@PathParam("layout_id")
     *            布局ID
     * @return 布局列实体列表
     * @author chenxiang
     */
    @GET
    @Path("/{layout_id}/item")
    public String getItems(@PathParam("layout_id")
    long layoutId)
    {
        try
        {
            DemoLayout dl = new DemoLayout(qr, owner);
            RowSet rs = dl.setLayoutId(layoutId).getItems(pageSize, pageNo, condition, orderField, fields);
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                map.put(Constants.TOTAL, dl.getLayoutItemCount(condition));
            }
            return ok(rs, map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 描述：获取布局列表。<br>
     * 
     * @GET
     * @return 所有布局列表与对应的列数量
     * @author chenxiang
     */
    @GET
    public String gets()
    {
        try
        {
            DemoLayout dl = new DemoLayout(qr, owner);
            RowSet rs = dl.getLayoutRowSet(pageSize, pageNo, condition, orderField, fields);
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                map.put(Constants.TOTAL, dl.getLayoutCount(condition));
            }
            return ok(dl.rsToList(rs), map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 修改一个布局demo。<br>
     * 
     * @PUT
     * @Path ("/{layout_id}")
     * @param layoutId-@PathParam("layout_id")
     *            布局ID
     * @param title-@FormParam("title")
     *            布局名称
     * @param container-@FormParam("container")
     *            布局容器，一般同一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            布局dome样例
     * @param shape-@FormParam("shape")
     *            布局样式图片
     * @param description-@FormParam("description")
     *            布局描述
     * @return 新增的布局实体
     * @author chenxiang
     */
    @PUT
    @Path("/{layout_id}")
    public String update(@PathParam("layout_id")
    long layoutId, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoLayout dl = new DemoLayout(qr, owner);
            dl.setLayoutId(layoutId);
            StringBuilder condition = new StringBuilder();
            if (title != null && !"".equals(title.trim()))
            {
                condition.append("title,");
                dl.setTitle(title);
            }
            if (container != null && !"".equals(container.trim()))
            {
                condition.append("container,");
                dl.setContainer(container);
            }
            dl.setTitle(title).setContainer(container);
            if (demo != null && !"".equals(demo.trim()))
            {
                condition.append("demo,");
                dl.setDemo(demo);
            }
            if (shape != null && !"".equals(shape.trim()))
            {
                condition.append("shape,");
                dl.setShape(shape);
            }
            if (description != null && !"".equals(description.trim()))
            {
                condition.append("description,");
                dl.setDescription(description);
            }

            if (condition.length() > 0)
            {
                dl.update(condition.toString());
            }
            else
            {
                dl.update();
            }
            return ok(dl);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }
}
